package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentCheckTaskDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentCheckTaskDetailParam;
import com.mxpio.erp.common.equipment.entity.EquipmentCommParametersDetail;
import com.mxpio.erp.common.equipment.service.EquipmentCheckTaskDetailParamService;
import com.mxpio.erp.common.equipment.service.EquipmentCheckTaskDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentCommParametersDetailService;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 点检任务明细
 * @Author: jeecg-boot
 * @Date:   2021-09-23
 * @Version: V1.0
 */
@Service
public class EquipmentCheckTaskDetailServiceImpl extends BaseServiceImpl<EquipmentCheckTaskDetail> implements EquipmentCheckTaskDetailService {

    @Autowired
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;
    @Autowired
    private EquipmentCheckTaskDetailParamService equipmentCheckTaskDetailParamService;


	@Override
	public List<EquipmentCheckTaskDetail> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("taskId", Operator.EQ, mainId);
        return list(EquipmentCheckTaskDetail.class,c);
	}

    // todo 待完成功能
//    @Override
//    public List<EquipmentCheckTaskDetailExport> selectExportByMainId(String mainId) {
//		return equipmentCheckTaskDetailMapper.selectExportByMainId(mainId);
//    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDetail(EquipmentCheckTaskDetail equipmentCheckTaskDetail) {
        save(equipmentCheckTaskDetail);

        // 处理计划明细中的选项数据
        if (equipmentCheckTaskDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> equipmentCommParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentCheckTaskDetail.getParamId());
            List<EquipmentCheckTaskDetailParam> checkTaskDetailParams = equipmentCommParametersDetails.stream().map(commParam -> {
                EquipmentCheckTaskDetailParam taskDetailParam = new EquipmentCheckTaskDetailParam();
                taskDetailParam.setParam(commParam.getParameter());
                taskDetailParam.setTaskId(equipmentCheckTaskDetail.getTaskId());
                taskDetailParam.setNum(commParam.getNum());
                taskDetailParam.setDetailId(equipmentCheckTaskDetail.getId());
                return taskDetailParam;
            }).collect(Collectors.toList());
            equipmentCheckTaskDetailParamService.save(checkTaskDetailParams);
        }
    }
}
